﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class GoodsAddForm : Form
    {
        public GoodsAddForm()
        {
            InitializeComponent();
            this.Opacity = 0;
            goodsType type = new goodsType();
            DataTable dt = type.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cbxgoodsTypeId.Items.Contains(dt.Rows[i]["typeName"].ToString()))
                {
                    cbxgoodsTypeId.Items.Add(dt.Rows[i]["typeName"].ToString());

                }
            }
            this.rbtnNo.Checked = true;
        }
        ProduceForm Form;
        public GoodsAddForm(ProduceForm Form1)
        {
            InitializeComponent();
            Form = Form1;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            goodsType type = new goodsType();
            DataTable dt = type.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cbxgoodsTypeId.Items.Contains(dt.Rows[i]["typeName"].ToString()))
                {
                    cbxgoodsTypeId.Items.Add(dt.Rows[i]["typeName"].ToString());

                }
            }
            this.rbtnNo.Checked = true;
        }


        
        private void GoodsAddForm_Load(object sender, EventArgs e)
        {
           
            this.timer1.Start();
            if(Form.ID!=0)
            {
               this.Text = "修改产品";
               this.txtgoodsId.ReadOnly = true;
            }
            DataBind();
        }
        private void DataBind()
        {
            
            cbxunit.Items.Add("千克");
            cbxunit.Items.Add("瓶");
            cbxunit.Items.Add("包");
            cbxunit.Items.Add("袋");
            cbxunit.Items.Add("支");

            

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtgoodsId.Text == "")
            {
                MessageBox.Show("商品编号不能为空!"); return;
            }
            if (txtgoodsName.Text == "")
            {
                MessageBox.Show("商品名称不能为空!"); return;
            }
            if (cbxgoodsTypeId.Text == "")
            {
                MessageBox.Show("商品类别不能为空!"); return;
            }
            if (txtamount.Text == "")
            {
                MessageBox.Show("商品数量不能为空"); return;
            }
            if (this.cbxunit.Text == "")
            {
                MessageBox.Show("商品单位不能为空!"); return;
            }
            if (txtprice.Text == "")
            {
                MessageBox.Show("商品售价不能为空!"); return;
            }
            try
            {
                goods good = new goods();
                if (this.Text != "修改产品")
                {
                     
                    if (good.Existsbyid(txtgoodsId.Text, this.txtgoodsName.Text))
                    {
                        MessageBox.Show("商品编号不能重复！");
                        return;
                    }
                }
                Model.goods goodModel = new Model.goods();
                if (this.Text == "修改产品")
                {
                    goodModel = good.GetModelList("id='" + Form.ID + "'")[0];
                }
                bool Promotion = true;
                if (this.rbtnNo.Checked == true)
                {
                    Promotion = false;
                }

                goodModel.goodsName = txtgoodsName.Text;
                goodModel.goodsId = txtgoodsId.Text;
                goodsType type = new goodsType();
                Model.goodsType typeModel = new Model.goodsType();
                typeModel = type.GetModelList("typeName='" + cbxgoodsTypeId.Text + "'")[0];

                goodModel.goodsTypeId = typeModel.id.ToString();

                goodModel.unit = cbxunit.Text;
                goodModel.Promotion = Promotion;



                goodModel.price = decimal.Parse(txtprice.Text);
                goodModel.Promotionprice = this.txtPromotionprice.Text;

                goodModel.amount = int.Parse(txtamount.Text);
                goodModel.tichengType = "";
                goodModel.finalCost =Convert.ToDouble(this.txtfinalCost.Text);
                foreach (RadioButton item in panel2.Controls)
                {
                    if (item.Checked == true)
                    {
                        goodModel.tichengType = item.Text;
                        break;
                    }
                }







                //商品流水信息
                goodsflowing goods = new goodsflowing();
                Model.goodsflowing goodsModel = new Model.goodsflowing();
                MainForm mm = new MainForm();
                goodsModel.username = MainForm.Operator;//用户名
                goodsModel.Time = DateTime.Now;
                if (this.Text == "修改产品")
                {
                    if (good.Update(goodModel))
                    {
                        goodsModel.operate = "商品修改";
                        goods goodd = new goods();
                        DataTable dtt = goodd.GetList("").Tables[0];
                        Form.dgvgoods.DataSource = dtt;
                        Form.dgvgoods.AllowUserToAddRows = false;
                        //Form.cuxiaoxianshi();
                        Form.Loaddgv();
                        this.Close();

                    }
                }
                else
                {
                    int row = good.Add(goodModel);
                    if (row > 0)
                    {

                        goodsModel.operate = "商品添加";
                        goods goodd = new goods();
                        DataTable dtt = goodd.GetList("").Tables[0];
                        Form.dgvgoods.DataSource = dtt;
                        Form.dgvgoods.AllowUserToAddRows = false;

                        Form.Loaddgv();
                        this.Close();
                    }
                }
                //商品流水信息
                goodsModel.username = MainForm.Operator;
                goodsModel.goodsId = txtgoodsId.Text;//商品编号
                goodsModel.goodsName = txtgoodsName.Text; //商品名称
                goodsModel.unit = cbxunit.Text;
                goodsModel.price = decimal.Parse(txtprice.Text);//预售单价

                goodsModel.amount = int.Parse(txtamount.Text); //数量
                goodsModel.Promotionprice = this.txtPromotionprice.Text;
                goodsModel.goodsTypeId = cbxgoodsTypeId.Text;//商品类别;
                int rowCount = goods.Add(goodsModel);
                if (rowCount <= 0)
                {
                    MessageBox.Show("系统出现故障！请检查."); return;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }
        int flag = 0;
        private void GoodsAddForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void txtgoodsId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void txtfinaCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                    else
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void txtprice_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            //{
            //    if (e.KeyChar == '.')
            //    {
            //        if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
            //            e.Handled = true;
            //    }
            //    else
            //        e.Handled = true;
            //}
            //else
            //{
            //    if (e.KeyChar <= 31)
            //    {
            //        e.Handled = false;
            //    }
            //    else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
            //    {
            //        if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
            //            e.Handled = true;
            //    }
            //}
        }

        private void txtamount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                    else
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void txtlowestoff_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                e.Handled = true;
            }   
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void rbtnyes_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbtnNo_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (RadioButton item in panel2.Controls)
            {
               
                if (item.Checked == true)
                {
                    MessageBox.Show(item.Text);
                }
            }
        }

        //private void rbtnyes_CheckedChanged(object sender, EventArgs e)
        //{
        //    //if (this.rbtnyes.Checked)
        //    //{
        //    //    label11.Visible = false;
        //    //    label6.Visible = false;
        //    //    label10.Visible = false;


        //    //    this.txtcuxiaojine.Visible = false;
        //    //    txtlowestoff.Visible = false;
               
        //    //}
        //    //else if (this.rbtnNo.Checked)
        //    //{
        //    //    label11.Visible = true;
        //    //    label6.Visible = true;
        //    //    label10.Visible = true;
        //    //    this.txtcuxiaojine.Visible = true;
        //    //    txtlowestoff.Visible = true;
                
        //    //}
            
        //}

        
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    if (txtgoodsId.Text == "")
        //    {
        //        MessageBox.Show("商品编号不能为空!"); return;
        //    }
        //    if (txtgoodsName.Text == "")
        //    {
        //        MessageBox.Show("商品名称不能为空!"); return;
        //    }
        //    if (cbxgoodsTypeId.Text == "")
        //    {
        //        MessageBox.Show("商品类别不能为空!"); return;
        //    }
        //    if (txtamount.Text == "")
        //    {
        //        MessageBox.Show("商品数量不能为空"); return;
        //    }
        //    if (txtfinaCost.Text == "")
        //    {
        //        MessageBox.Show("单价成本不能为空!"); return;
        //    }
        //    if (txtprice.Text == "")
        //    {
        //        MessageBox.Show("商品售价不能为空!"); return;
        //    }
        //    try
        //    {
        //        goods good = new goods();
        //        Model.goods goodModel = new Model.goods();
        //        if (this.Text == "修改产品")
        //        {
        //            goodModel = good.GetModelList("id='" + Form.ID + "'")[0];
        //        }
        //        if (txtlowestoff.Text != "")
        //        {
        //            goodModel.lowestOff = txtlowestoff.Text;
        //        }
        //        else
        //        {
        //            goodModel.lowestOff = "100";
        //        }

        //        goodModel.goodsName = txtgoodsName.Text;
        //        goodModel.goodsId = txtgoodsId.Text;
        //        goodsType type = new goodsType();
        //        Model.goodsType typeModel = new Model.goodsType();
        //        typeModel = type.GetModelList("typeName='" + cbxgoodsTypeId.Text + "'")[0];

        //        goodModel.goodsTypeId = typeModel.id.ToString();

        //        goodModel.unit = cbxunit.Text;
        //        goodModel.finalCost = decimal.Parse(txtfinaCost.Text);
        //        goodModel.price = decimal.Parse(txtprice.Text);

        //        goodModel.amount = int.Parse(txtamount.Text);










        //        //商品流水信息
        //        goodsflowing goods = new goodsflowing();
        //        Model.goodsflowing goodsModel = new Model.goodsflowing();
        //        MainForm mm = new MainForm();
        //        goodsModel.username = MainForm.Operator;//用户名
        //        goodsModel.Time = DateTime.Now;

        //        if (this.Text == "修改产品")
        //        {
        //            if (good.Update(goodModel))
        //            {
        //                goodsModel.operate = "商品修改";
        //                goods goodd = new goods();
        //                DataTable dtt = goodd.GetList("").Tables[0];
        //                Form.dgvgoods.DataSource = dtt;
        //                Form.dgvgoods.AllowUserToAddRows = false;
        //                foreach (DataGridViewRow dgv in Form.dgvgoods.Rows)
        //                {
        //                    goodsType goodtype = new goodsType();
        //                    Model.goodsType goodtypeModel = new Model.goodsType();
        //                    goodtypeModel = goodtype.GetModelList("id='" + dgv.Cells["goodsTypeId"].Value + "'")[0];
        //                    dgv.Cells["typeName1"].Value = goodtypeModel.typeName;
        //                }
        //                this.Close();
        //            }
        //        }
        //        else
        //        {
        //            int row = good.Add(goodModel);
        //            if (row > 0)
        //            {

        //                goodsModel.operate = "商品添加";
        //                goods goodd = new goods();
        //                DataTable dtt = goodd.GetList("").Tables[0];
        //                Form.dgvgoods.DataSource = dtt;
        //                Form.dgvgoods.AllowUserToAddRows = false;
        //                foreach (DataGridViewRow dgv in Form.dgvgoods.Rows)
        //                {
        //                    goodsType goodtype = new goodsType();
        //                    Model.goodsType goodtypeModel = new Model.goodsType();
        //                    goodtypeModel = goodtype.GetModelList("id='" + dgv.Cells["goodsTypeId"].Value + "'")[0];
        //                    dgv.Cells["typeName1"].Value = goodtypeModel.typeName;
        //                }
        //                this.Close();
        //            }
        //        }
        //        //商品流水信息
        //        goodsModel.username = MainForm.Operator;
        //        goodsModel.goodsId = txtgoodsId.Text;//商品编号
        //        goodsModel.goodsName = txtgoodsName.Text; //商品名称
        //        goodsModel.unit = cbxunit.Text;
        //        goodsModel.price = decimal.Parse(txtprice.Text);//预售单价
        //        goodsModel.finalCost = decimal.Parse(txtfinaCost.Text); //成本
        //        goodsModel.amount = int.Parse(txtamount.Text); //数量
        //        goodsModel.lowestOff = txtlowestoff.Text;
        //        goodsModel.goodsTypeId = cbxgoodsTypeId.Text;//商品类别;
        //        int rowCount = goods.Add(goodsModel);
        //        if (rowCount <= 0)
        //        {
        //            MessageBox.Show("系统出现故障！请检查."); return;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Windows.Forms.MessageBox.Show(ex.Message);
        //    }
        //}
    }
}